package com.example.magictowerx;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

public class HelpScreen extends GameView
{
    private MainGame	mMainGame	= null;
    private Paint		paint		= null;
    private TextUtil	title		= null;
    private TextUtil	tu			= null;


    public HelpScreen(Context context, MainGame mainGame)
    {
        super(context);
        mMainGame = mainGame;
        paint = new Paint();
        title = new TextUtil();
        tu = new TextUtil();
        Resources r = this.getContext().getResources();
        title.InitText((r.getString(R.string.t_help)), (int)(MySet.SCREENW - paint.measureText((r.getString(R.string.t_about)))) / 2, 0, MySet.SCREENW, MySet.SCREENH, 0x0, 0xff0000, 255, MySet.TextSize);
        tu.InitText(((String) r.getString(R.string.help)), 0, 0, MySet.SCREENW, MySet.SCREENH, 0x0, 0xff0000, 255, MySet.TextSize);
    }


    protected void onDraw(Canvas canvas)
    {
        paint.setColor(Color.BLACK);
        MySet.fillRect(canvas, 0, 0, MySet.SCREENW, MySet.SCREENH, paint);
        drawCont(canvas);
        title.SetTextColor(0xffff00);
        tu.SetTextColor(0xffff00);
        title.DrawText(canvas);
        tu.DrawText(canvas);
    }

    private void drawCont(Canvas canvas) //操作栏
    {
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_WHEEL), 160, 1350, 400, 400, 0, 0);
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_BTNA), 600, 1380, 240, 240, 0, 0);
        MySet.drawImage(canvas, getImage(GameScreen.IMAGE_BTNB), 800, 1520, 240, 240, 0, 0);
    }

    public Bitmap getImage(int type)
    {
        Bitmap result = null;
        switch (type)
        {
            case  GameScreen.IMAGE_WHEEL:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.wheel_1);
                break;
            case  GameScreen.IMAGE_BTNA:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btna_1);
                break;
            case  GameScreen.IMAGE_BTNB:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btnb_1);
                break;
        }
        return result;
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int type = 0;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 记录触摸点坐标
                if (MySet.touchUp(x,y) )
                {
                    tu.Key(MySet.KEY_DPAD_UP);
                }
                else if (MySet.touchDown(x,y))
                {
                    tu.Key(MySet.KEY_DPAD_DOWN);
                }
                else
                {
                    mMainGame.controlView(MySet.GAME_MENU);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    public boolean onKeyUp(int keyCode)
    {
        if (keyCode == MySet.KEY_DPAD_UP || keyCode == MySet.KEY_DPAD_DOWN)
        {
            tu.Key(keyCode);
        }
        else
        {
            mMainGame.controlView(MySet.GAME_MENU);
        }
        return true;
    }


    public boolean onKeyDown(int keyCode)
    {
        return true;
    }


    public void refurbish()
    {

    }


    public void reCycle()
    {
        paint = null;
        title = null;
        tu = null;
        System.gc();
    }
}

